package com.example.navigationpage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerTabStrip;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    //存放view的数组
    private ArrayList<View> mViewList;
    //适配器类
    private NavPagerAdapter mAdapter;
    //存放标题的数组
    private ArrayList<String> Titles;
    //PagerTabStrip
    private PagerTabStrip PagerTab;
    //ViewPager
    private ViewPager mViewPager;
    private TextView mJumpTv;
    private static final String TAG = "MainActivityLog";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initData();
        initView();

    }

    /**
     * viewpager设置适配器
     * 显示视图
     */
    private void initView() {
        //取得ViewPager实例
        mViewPager = findViewById(R.id.ViewPager);
        mViewPager.setOnPageChangeListener(this);
        //取得ViewTabStrip实例 之前是没有用过ViewTabStrip的，试着用了一下，看了下效果
//        PagerTab = (PagerTabStrip) findViewById(R.id.PagerTab);
//        //设置Tab选中时的颜色
//        PagerTab.setTabIndicatorColor(getResources().getColor(R.color.black));
//        //设置Tab是否显示下划线
//        PagerTab.setDrawFullUnderline(true);
//        //设置Tab背景色
//        PagerTab.setBackgroundColor(getResources().getColor(R.color.white));
//        //设置Tab间的距离？我感觉是这样
//        PagerTab.setTextSpacing(50);

        //初始化适配器
//        mAdapter = new NavPagerAdapter(mViewList, Titles);
        mAdapter = new NavPagerAdapter(mViewList);

        mViewPager.setAdapter(mAdapter);

        mJumpTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "可以跳了", Toast.LENGTH_SHORT).show();
            }
        });

        //默认显示第1项
        SetTab(0);

    }
    /**
     * 初始化viewpager控件上
     * 需要显示的视图集合
     */
    private void initData() {

        mViewList = new ArrayList<>();
        LayoutInflater Inflater = LayoutInflater.from(this);
        mViewList.add(Inflater.inflate(R.layout.item_viewpaper_first, null));
        mViewList.add(Inflater.inflate(R.layout.item_viewpaper_second, null));
        mViewList.add(Inflater.inflate(R.layout.item_viewpaper_third, null));
        View view = Inflater.inflate(R.layout.item_viewpaper_fourth, null);
        mJumpTv = view.findViewById(R.id.jump_tv);
        mViewList.add(view);

        //初始化Titles
//        Titles = new ArrayList<String>();
//        Titles.add("Tab1");
//        Titles.add("Tab2");
//        Titles.add("Tab3");
//        Titles.add("Tab4");
    }
    /**
     * 设置默认显示的页数
     * 比如一共4页 就是 从0-3，按照下标来
     * @param index 页数集合的下标
     */
    public void SetTab(int index) {
        switch (index) {
            case 0:
                mViewPager.setCurrentItem(0);
                break;
            case 1:
                mViewPager.setCurrentItem(1);
                break;
            case 2:
                mViewPager.setCurrentItem(2);
                break;
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        SetTab(position);
        Log.d(TAG, "onPageSelected: position:" + position);

    }

    @Override
    public void onPageScrollStateChanged(int state) {

        Log.d(TAG, "onPageScrollStateChanged: state:" + state);

    }
}